home *** CD-ROM | disk | FTP | other *** search
- /* textps.c */
- /************************************************************************
- * This code is from the lprps package by James Clark *
- ************************************************************************/
-
- #ifdef TEXTPS
-
- #include "LPD.H"
- #include "lpdProtos.H"
-
- #define A4
-
- typedef struct output_char {
- struct output_char *next;
- char c;
- char is_bold;
- int pos;
- } output_char;
-
- output_char *free_output_char_list = 0;
-
- int tab_width = 8;
- #ifdef A4
- double page_length = 842.0; /* in points */
- int lines_per_page = 66;
- #else
- double page_length = 792.0; /* in points */
- int lines_per_page = 66;
- #endif
-
-
- //double baseline_offset = 8.0; /* distance in points from top of page to */
- double baseline_offset = 28.0; /* distance in points from top of page to */
- /* first baseline */
-
- double vertical_spacing = 12.0; /* in points */
-
- double left_margin = 18.0; /* in points */
- double char_width;
-
- int pageno = 0;
-
- char _fgetc(integer f);
-
- #define EOF 0
-
- char *font = "Courier";
- char *bold_font = "Courier-Bold";
- enum { NONE, ROMAN, BOLD } current_font;
-
- void do_file(integer);
- void prologue(void);
- void trailer(void);
- char *prog;
-
-
- static integer fRef;
- double cpi = 12.0;
- static LongInt size, done;
-
- /************************************************************************
- ************************************************************************/
- integer textps(StringPtr name, integer refNum)
- {
- integer err;
- extern integer spool_dir;
- char_width = 72.0/cpi;
-
- CVT_Banner_Up();
-
- Create("\p%tmp_file%", spool_dir, 'SIMN', 'TEXT');
- if (err = FSOpen("\p%tmp_file%", spool_dir, &fRef))
- FileError("\pOpening scratch file ", err);
-
- GetEOF(refNum, &size);
- done = 0;
-
- prologue();
- do_file(refNum);
- if (!stopped) trailer();
- else SetEOF(fRef, 0);
-
- FSClose(refNum);
- FSClose(fRef);
-
- FSDelete(name, spool_dir); // delete the old one
- Rename("\p%tmp_file%", spool_dir, name); // rename the new one
-
- if (err = FSOpen(name, spool_dir, &fRef))
- FileError("\pRe-opening scratch file ", err);
-
- CVT_Banner_Down();
-
- return fRef;
- }
-
-
- /************************************************************************
- ************************************************************************/
- output_char *new_output_char(void);
- output_char *new_output_char()
- {
- integer state = 0;
-
- WaitForState(&state);
-
- if (free_output_char_list)
- {
- output_char *tem = free_output_char_list;
- free_output_char_list = free_output_char_list->next;
- return tem;
- }
- else
- {
- output_char *tem;
- if ((tem = (output_char *)NewPtr(sizeof(output_char))) == NULL)
- {
- log_printf("%s: out of memory\n", prog);
- }
- return tem;
- }
- }
-
- static Boolean last_was_cr = FALSE;
-
- /************************************************************************
- ************************************************************************/
- static char _fgetc(integer fRef)
- {
- Byte c;
- LongInt count = 1;
- integer state = 0;
-
- WaitForState(&state);
- if (stopped) return EOF;
-
- if (FSRead(fRef, &count, &c))
- return EOF;
-
- CVT_Banner_PCent( ++done, size );
-
- if (c == CR)
- last_was_cr = TRUE;
- else if (c == LF)
- {
- if (last_was_cr)
- {
- last_was_cr = FALSE;
- return _fgetc(fRef);
- }
- else c = CR;
- last_was_cr = FALSE;
- }
-
- return (char)(c & 0x7F);
- }
-
-
- /************************************************************************
- ************************************************************************/
- void delete_output_char(output_char *p);
- void delete_output_char(output_char *p)
- {
- p->next = free_output_char_list;
- free_output_char_list = p;
- }
-
- #define isascii(c) (!((c) & ~0177))
- #define iscntrl(c) ( (c) > 0 && (c) < ' ' )
-
- /************************************************************************
- ************************************************************************/
- void pschar(int c);
- void pschar(int c)
- {
- if (!isascii(c) || iscntrl(c))
- fprintf(fRef, "\\%03o", c & 0377);
- else if (c == '(' || c == ')' || c == '\\')
- {
- fputc(fRef, '\\');
- fputc(fRef, c);
- }
- else
- fputc(fRef, c);
- }
-
- /************************************************************************
- ************************************************************************/
- void psnum(double f);
- void psnum(double f)
- {
- LongInt l = f;
-
- fprintf(fRef, "%ld", l);
- }
-
- /* output_line is ordered greatest position first */
-
- /************************************************************************
- ************************************************************************/
- void print_line(output_char *output_line, int vpos);
- void print_line(output_char *output_line, int vpos)
- {
- output_char *rev = output_line;
- output_line = 0;
- while (rev != 0)
- {
- output_char *tem = rev;
- rev = rev->next;
- tem->next = output_line;
- output_line = tem;
- }
- while (output_line != NULL)
- {
- output_char *tem;
- output_char **p = &output_line;
- int start_pos = output_line->pos;
- int is_bold = output_line->is_bold;
- int pos;
- if (is_bold)
- {
- if (current_font != BOLD)
- {
- fprintf(fRef, "B");
- current_font = BOLD;
- }
- }
- else
- {
- if (current_font != ROMAN)
- {
- fprintf(fRef, "R");
- current_font = ROMAN;
- }
- }
- fputc(fRef, '(');
- pschar(output_line->c);
- pos = output_line->pos + 1;
- tem = output_line;
- output_line = output_line->next;
- delete_output_char(tem);
- for (;;)
- {
- while (*p != NULL
- && ((*p)->pos < pos || (*p)->is_bold != is_bold))
- p = &(*p)->next;
- if (*p == NULL)
- break;
- while (pos < (*p)->pos)
- {
- pschar(' ');
- pos++;
- }
- pschar((*p)->c);
- pos++;
- tem = *p;
- *p = tem->next;
- delete_output_char(tem);
- }
- fputc(fRef, ')');
- psnum(left_margin + start_pos*char_width);
- fputc(fRef, ' ');
- psnum(page_length - baseline_offset - vpos*vertical_spacing);
- fprintf(fRef, " L\n");
- }
- }
-
- /************************************************************************
- ************************************************************************/
- void page_start(void);
- void page_start()
- {
- fprintf(fRef, "%%%%Page: ? %d\n%%%%BeginPageSetup\nPS\n%%%%EndPageSetup\n",
- ++pageno);
- current_font = NONE;
- }
-
- /************************************************************************
- ************************************************************************/
- void page_end(void);
- void page_end()
- {
- fprintf(fRef, "PE\n");
- }
-
-
- /************************************************************************
- ************************************************************************/
- char *latin1[] = {
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- "space",
- "exclam",
- "quotedbl",
- "numbersign",
- "dollar",
- "percent",
- "ampersand",
- "quoteright",
- "parenleft",
- "parenright",
- "asterisk",
- "plus",
- "comma",
- "hyphen",
- "period",
- "slash",
- "zero",
- "one",
- "two",
- "three",
- "four",
- "five",
- "six",
- "seven",
- "eight",
- "nine",
- "colon",
- "semicolon",
- "less",
- "equal",
- "greater",
- "question",
- "at",
- "A",
- "B",
- "C",
- "D",
- "E",
- "F",
- "G",
- "H",
- "I",
- "J",
- "K",
- "L",
- "M",
- "N",
- "O",
- "P",
- "Q",
- "R",
- "S",
- "T",
- "U",
- "V",
- "W",
- "X",
- "Y",
- "Z",
- "bracketleft",
- "backslash",
- "bracketright",
- "asciicircum",
- "underscore",
- "quoteleft",
- "a",
- "b",
- "c",
- "d",
- "e",
- "f",
- "g",
- "h",
- "i",
- "j",
- "k",
- "l",
- "m",
- "n",
- "o",
- "p",
- "q",
- "r",
- "s",
- "t",
- "u",
- "v",
- "w",
- "x",
- "y",
- "z",
- "braceleft",
- "bar",
- "braceright",
- "asciitilde",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- ".notdef",
- "dotlessi",
- "grave",
- "acute",
- "circumflex",
- "tilde",
- "macron",
- "breve",
- "dotaccent",
- "dieresis",
- ".notdef",
- "ring",
- "cedilla",
- ".notdef",
- "hungarumlaut",
- "ogonek",
- "caron",
- ".notdef",
- "exclamdown",
- "cent",
- "sterling",
- "currency",
- "yen",
- "brokenbar",
- "section",
- "dieresis",
- "copyright",
- "ordfeminine",
- "guilsinglleft",
- "logicalnot",
- "registered",
- "minus",
- "macron",
- "degree",
- "plusminus",
- "twosuperior",
- "threesuperior",
- "acute",
- "mu",
- "paragraph",
- "periodcentered",
- "cedilla",
- "onesuperior",
- "ordmasculine",
- "guilsinglright",
- "onequarter",
- "onehalf",
- "threequarters",
- "questiondown",
- "Agrave",
- "Aacute",
- "Acircumflex",
- "Atilde",
- "Adieresis",
- "Aring",
- "AE",
- "Ccedilla",
- "Egrave",
- "Eacute",
- "Ecircumflex",
- "Edieresis",
- "Igrave",
- "Iacute",
- "Icircumflex",
- "Idieresis",
- "Eth",
- "Ntilde",
- "Ograve",
- "Oacute",
- "Ocircumflex",
- "Otilde",
- "Odieresis",
- "multiply",
- "Oslash",
- "Ugrave",
- "Uacute",
- "Ucircumflex",
- "Udieresis",
- "Yacute",
- "Thorn",
- "germandbls",
- "agrave",
- "aacute",
- "acircumflex",
- "atilde",
- "adieresis",
- "aring",
- "ae",
- "ccedilla",
- "egrave",
- "eacute",
- "ecircumflex",
- "edieresis",
- "igrave",
- "iacute",
- "icircumflex",
- "idieresis",
- "eth",
- "ntilde",
- "ograve",
- "oacute",
- "ocircumflex",
- "otilde",
- "odieresis",
- "divide",
- "oslash",
- "ugrave",
- "uacute",
- "ucircumflex",
- "udieresis",
- "yacute",
- "thorn",
- "ydieresis",
- };
-
- /************************************************************************
- ************************************************************************/
- void prologue()
- {
- integer col, i, len;
- char *ls;
-
- fprintf(fRef, "%%!PS-Adobe-3.0\n");
- fprintf(fRef, "%%%%DocumentNeededResources: font %s\n", font);
- fprintf(fRef, "%%%%+ font %s\n", bold_font);
- fprintf(fRef, "%%%%Pages: (atend)\n");
- fprintf(fRef, "%%%%EndComments\n");
- fprintf(fRef, "%%%%BeginProlog\n");
- fprintf(fRef, "/textps 10 dict def textps begin\n");
-
- fprintf(fRef, "/L { moveto show } bind def\n");
- fprintf(fRef, "/PS { /level0 save def } bind def\n");
- fprintf(fRef, "/PE { level0 restore showpage } bind def\n");
- fprintf(fRef, "/RE {\n");
- fprintf(fRef, "\tfindfont\n");
- fprintf(fRef, "\tdup maxlength dict begin\n");
- fprintf(fRef, "\t{\n");
- fprintf(fRef, "\t\t1 index /FID ne { def } { pop pop } ifelse\n");
- fprintf(fRef, "\t} forall\n");
- fprintf(fRef, "\t/Encoding exch def\n");
- fprintf(fRef, "\tdup /FontName exch def\n");
- fprintf(fRef, "\tcurrentdict end definefont pop\n");
- fprintf(fRef, "} bind def\n");
-
- fprintf(fRef, "/ISOLatin1Encoding where{pop}{/ISOLatin1Encoding[\n");
-
- col = 0;
- for (i = 0; i < 256; i++)
- {
- ls = latin1[i];
- len = strlen(ls) + 1;
- col += len;
- if (col > 79)
- {
- // fputc(fRef, '\n');
- fputc(fRef, CR);
- col = len;
- }
- fprintf(fRef, "/%s", ls);
- }
-
- fprintf(fRef, "\n] def}ifelse\nend\n");
- fprintf(fRef, "%%%%BeginSetup\n");
- fprintf(fRef, "%%%%IncludeResource: font %s\n", font);
- fprintf(fRef, "%%%%IncludeResource: font %s\n", bold_font);
- fprintf(fRef, "textps begin\n");
- fprintf(fRef, "/__%s ISOLatin1Encoding /%s RE\n", font, font);
- fprintf(fRef, "/R [ /__%s findfont ", font);
- psnum(char_width/.6);
- fprintf(fRef, " scalefont /setfont load ] cvx def\n");
- fprintf(fRef, "/__%s ISOLatin1Encoding /%s RE\n", bold_font, bold_font);
- fprintf(fRef, "/B [ /__%s findfont ", bold_font);
- psnum(char_width/.6);
- fprintf(fRef, " scalefont /setfont load ] cvx def\n");
- fprintf(fRef, "%%%%EndSetup\n");
- fprintf(fRef, "%%%%EndProlog\n");
- }
-
- /************************************************************************
- ************************************************************************/
- void trailer()
- {
- fprintf(fRef, "%%%%Trailer\nend\n%%%%Pages: %d\n", pageno);
- }
-
- /* p is ordered greatest position first */
-
- /************************************************************************
- ************************************************************************/
- void add_char(int c, int pos, output_char **p);
- void add_char(int c, int pos, output_char **p)
- {
- for (;; p = &(*p)->next)
- {
- if (*p == NULL || (*p)->pos < pos)
- {
- output_char *tem = new_output_char();
- tem->next = *p;
- *p = tem;
- tem->c = c;
- tem->is_bold = 0;
- tem->pos = pos;
- break;
- }
- else if ((*p)->pos == pos)
- {
- if (c == (*p)->c)
- {
- (*p)->is_bold = 1;
- break;
- }
- }
- }
- }
-
- /************************************************************************
- ************************************************************************/
- void do_file(integer fp)
- {
- int c;
- int vpos = 0;
- int hpos = 0;
- int page_started = 0;
- int esced = 0;
-
- output_char *output_line = 0;
- while ((c = _fgetc(fp)) != EOF)
- {
- MainEvents(everyEvent);
- if (esced)
- {
- switch(c)
- {
- case '7':
- if (vpos > 0)
- {
- if (output_line != NULL)
- {
- if (!page_started)
- {
- page_started = 1;
- page_start();
- }
- print_line(output_line, vpos);
- output_line = 0;
- }
- vpos -= 1;
- }
- /* hpos = 0; */
- esced = 0;
- break;
- default:
- /* silently ignore */
- esced = 0;
- break;
- }
- }
- else
- {
- switch (c)
- {
- case '\033':
- esced = 1;
- break;
- case '\b':
- if (hpos > 0)
- hpos--;
- break;
- case '\f':
- if (!page_started)
- page_start();
- print_line(output_line, vpos);
- output_line = 0;
- page_end();
- hpos = 0; /* ?? */
- vpos = 0;
- page_started = 0;
- break;
- case CR:
- hpos = 0;
- break;
- case LF:
- if (output_line != NULL)
- {
- if (!page_started)
- {
- page_started = 1;
- page_start();
- }
- print_line(output_line, vpos);
- output_line = 0;
- }
- vpos += 1;
- if (vpos >= lines_per_page)
- {
- if (!page_started)
- page_start();
- page_end();
- page_started = 0;
- vpos = 0;
- }
- hpos = 0;
- break;
- case ' ':
- hpos++;
- break;
- case '\t':
- hpos = ((hpos + tab_width)/tab_width)*tab_width;
- break;
- default:
- if (!(isascii(c) && iscntrl(c)))
- {
- add_char(c, hpos, &output_line);
- hpos++;
- }
- break;
- }
- }
- }
- if (output_line != NULL)
- {
- if (!page_started)
- {
- page_started = 1;
- page_start();
- }
- print_line(output_line, vpos);
- output_line = 0;
- }
- if (page_started)
- page_end();
- }
- #endif
-